# -*- encoding: utf-8 -*-
# @File    :   article.py
# @Time    :   2021/07/27 14:18:43
# @Author  :   wudongmin
# @Contact :   wudongmin@xroom.net
# @version   :  1.0

from . import RepositoryBase
# from app.model.user import User
from app.model.article import Article, Animation, ArticleType
from app.items import PagniationItem, OrderItem


class RepositoryArticle(RepositoryBase):
    # article repository

    def __init__(self) -> None:
        self._list_item = [Article.id, Article.title, Article.description, Article.cover_url,
                           Article.created_at, ArticleType.title.label("ariticle_type_title")]
        super().__init__()

    def get_type_list(self) -> dict:
        # 获取文章类型列表
        res = self._session.query(ArticleType).order_by(ArticleType.id).all()
        for item in res:
            item.total = self._session.query(Article).filter_by(article_type_id=item.id).count()
        return res

    def get_list(self, page_params: PagniationItem = None, order_params: OrderItem = None) -> tuple([list, int]):
        # 获取文章列表
        f = self._session.query(*self._list_item).join(ArticleType)
        count = f.count()

        # 排序
        f = self.sort_filter(f, order_params=order_params)

        # 分页
        f = self.pagination(f, page_params=page_params)
        return f.all(), count

    def get_detail(self, id: int):
        # 获取文章详情
        res = self._session.query(*self._list_item+[Article.content]).filter_by(id=id).join(ArticleType).first()
        return res

    def get_animation_detail(self, title: str):
        res = self._session.query(Animation).get(title)
        return res
